package com.xinghou.mq.impl;

import com.xinghou.mq.UserInterface;
import com.xinghou.platform.dao.main.*;
import com.xinghou.platform.lucene.main.UserLuceneService;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.UserGeo;
import com.xinghou.platform.model.main.UserStatistics;
import com.xinghou.platform.service.main.UserService;
import com.xinghou.platform.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Closeable;
import java.io.IOException;

@Service("mqUserService")
public class UserInterfaceImpl implements UserInterface, Closeable {
	private static Logger logger = Logger.getLogger(UserInterfaceImpl.class);
	@Resource
	private UserDAO userDAO;
	@Resource
	private UserService userService;

	@Resource
	private CityDAO cityDAO;
	@Resource
	private StoreDAO storeDAO;
	
	@Resource
	private UserLuceneService userLuceneService;



	@Resource
	private UserStatisticsDAO userStatisticsDAO;

	@Resource
	private UserGeoDAO userGeoDAO;



	public UserInterfaceImpl() {

	}
	@Override
	public void update(int userId,boolean update) {
		User user = userDAO.getUserById(userId);
		if(user==null){
			logger.error("user is null ;userId="+userId+";update="+update);
			return ;
		}
		UserGeo userGeo=userGeoDAO.getUserGeoById(user.getUserid());
		if(userGeo!=null) {
			user.setLatitude(userGeo.getLatitude());
			user.setLongitude(userGeo.getLongitude());
			user.setCitycode(userGeo.getCitycode());
			if(!StringUtils.isEmpty(userGeo.getAdcode())) {
				user.setAd_code(userGeo.getAdcode());
				user.setAd_code_province(userGeo.getAdcode().substring(0,2)+"0000");
				user.setAd_code_city(userGeo.getAdcode().substring(0,4)+"00");
				user.setAd_code_region(userGeo.getAdcode());
			}
		}
		UserStatistics userStatistics=userStatisticsDAO.getUserStatById(user.getUserid());
		if(userStatistics!=null&&userStatistics.getSharecount()>0)
			user.setHascard("1");
		else
		user.setHascard("0");
        if(update){
        	userLuceneService.updateUserInfo(user);
        }else{
        	userLuceneService.saveUserInfo(user);
        }
	}

 

	@Override
	public void close() throws IOException {

	}

	@Override
	public void HuanXinRegister(Integer uid,String password) {
		userService.registerHuanxin(uid,password,"");

	}
}
